package com.sdb.deepseek.controller;

import com.sdb.deepseek.service.TongyiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;

@RestController
@RequestMapping("/ai/chat")
public class ChatController {

    @Autowired
    private TongyiService tongyiService;

    @PostMapping("/ask")
    public ResponseEntity<?> chat(@RequestBody Map<String, String> request) {
        String userInput = request.get("message");
        if (userInput == null || userInput.trim().isEmpty()) {
            return ResponseEntity.badRequest().body("请输入有效消息");
        }

        try {
            String response = tongyiService.getChatResponse(userInput);
            return ResponseEntity.ok().body(Collections.singletonMap("response", response));
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("服务异常: " + e.getMessage());
        }
    }
}